package com.py.credit.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.SysUser;
import com.py.credit.service.DoublePublicityService;
import com.py.credit.service.RemoteBaseService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

/**
 * 异议处理controller
 */
@RestController
@RequestMapping(value = "/doublePublicity")
public class DoublePublicityController {

	private final static String FRONT_TABLE_ID = "doublePublicity";

	@Autowired
	private DoublePublicityService doublePublicityService;

	@Autowired
	private RemoteBaseService remoteBaseService;

	@Value("${absolutepath}")
	private String absolutepath;

	/**
	 * 异议修改分页列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/xzxk.json")
	public RestServiceResult<List<Object[]>> xzxk(HttpServletRequest request) {
		RestServiceResult<List<Object[]>> result = new RestServiceResult<List<Object[]>>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.xzxk());
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	/**
	 * 异议修改分页列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/xzxkxyzt.json")
	public RestServiceResult<Map> xzxkxyzt(HttpServletRequest request) {
		RestServiceResult<Map> result = new RestServiceResult<Map>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.xzxkxyzt());
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	/**
	 * 异议修改分页列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/xzxktop.json")
	public RestServiceResult<List<Object[]>> xzxktop(HttpServletRequest request) {
		RestServiceResult<List<Object[]>> result = new RestServiceResult<List<Object[]>>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.xzxktop());
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	/**
	 * 异议修改分页列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/doublePublicityRate.json")
	public RestServiceResult<List<Object[]>> doublePublicityRate(HttpServletRequest request) {
		RestServiceResult<List<Object[]>> result = new RestServiceResult<List<Object[]>>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.doublePublicityRate());
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	/**
	 * 异议修改分页列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/xzcf.json")
	public RestServiceResult<List<Object[]>> xzcf(HttpServletRequest request) {
		RestServiceResult<List<Object[]>> result = new RestServiceResult<List<Object[]>>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.xzcf());
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	/**
	 * 异议修改分页列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/xzcfxyzt.json")
	public RestServiceResult<Map> xzcfxyzt(HttpServletRequest request) {
		RestServiceResult<Map> result = new RestServiceResult<Map>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.xzcfxyzt());
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	/**
	 * 异议修改分页列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/xzcftop.json")
	public RestServiceResult<List<Object[]>> xzcftop(HttpServletRequest request) {
		RestServiceResult<List<Object[]>> result = new RestServiceResult<List<Object[]>>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.xzcftop());
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	/**
	 * 异议修改分页列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/centralmapSingle.json")
	public RestServiceResult<List<Map<String, Object>>> centralmapSingle(HttpServletRequest request) {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();

		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.centralmapSingle());
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	/**
	 * 异议修改分页列表
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/centralmap.json")
	public RestServiceResult<List<Map>> centralmap(HttpServletRequest request) {
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.centralmapAll());
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	/**
	 * 
	 * @api {get} /creditMap/updateLonAndLat.json 经纬度更新定时任务
	 * @apiName updateLonAndLat
	 * @apiVersion 0.0.1
	 * @apiGroup 经纬度更新
	 */
	@RequestMapping(value = "/insertCentralMap.do")
	public RestServiceResult<Object> insertCentralMap() {
		try {
			doublePublicityService.insertCentralMap();
			return new RestServiceResult<Object>();
		} catch (Exception e) {
			RestServiceResult<Object> result = new RestServiceResult<Object>();
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("任务提醒定时任务异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("任务提醒定时任务DataRemindController#taskRemind", "任务提醒定时任务异常！ERROR:" + e.getMessage(), e);
			return result;
		}
	}

	@RequestMapping(value = "/countPercentByMeasure.do")
	public RestServiceResult<List<Map<String, Integer>>> countPercentByMeasure() {
		RestServiceResult<List<Map<String, Integer>>> result = new RestServiceResult<List<Map<String, Integer>>>();
		try {
			result.setData(doublePublicityService.countPercentByMeasure());
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("数据异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("任务提醒定时任务DoublePublicityController#countPercentByMeasure.do",
					"任务提醒定时任务异常！ERROR:" + e.getMessage(), e);
		}
		return result;
	}
	@IgnoreAuthority
	@RequestMapping(value = "/countPercentHhByMeasure.do")
	public RestServiceResult<List<Map<String, Integer>>> countPercentHhByMeasure() {
		RestServiceResult<List<Map<String, Integer>>> result = new RestServiceResult<List<Map<String, Integer>>>();
		try {
			result.setData(doublePublicityService.countPercentHhByMeasure());
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("数据异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("任务提醒定时任务DoublePublicityController#countPercentByMeasure.do",
					"任务提醒定时任务异常！ERROR:" + e.getMessage(), e);
		}
		return result;
	}
	@RequestMapping(value = "/countDataByDate.do")
	public RestServiceResult<Object> countDataByMonth() {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			result.setData(doublePublicityService.countDataByDate());
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("数据异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("任务提醒定时任务DoublePublicityController#countDataByDate.do",
					"任务提醒定时任务异常！ERROR:" + e.getMessage(), e);

			return result;
		}
	}
	@IgnoreAuthority
	@RequestMapping(value = "/countDataHhByDate.do")
	public RestServiceResult<Object> countDataHhByDate() {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			result.setData(doublePublicityService.countDataHhByDate());
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("数据异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("任务提醒定时任务DoublePublicityController#countDataHhByDate.do",
					"任务提醒定时任务异常！ERROR:" + e.getMessage(), e);

			return result;
		}
	}
	
	/**
	 * 双公示数据量汇总（日、周、月、年）
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/countSumByDate.json")
	public RestServiceResult<List<Map>> countSumByDate(HttpServletRequest request,String date) {
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.countSumByDate(date));
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}
	
	/**
	 * 双公示数据量按照部门排序（日、周、月、年）top3
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/countSumByDataAndDeptTop3.json")
	public RestServiceResult<List<Map>> countSumByDataAndDeptTop3(HttpServletRequest request,String date) {
		RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.countSumByDataAndDeptTop3(date));
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}
	
	/**
	 * 双公示数据量按照部门汇总（日、周、月、年）
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/countSumByDept.json")
	public RestServiceResult<Map> countSumByDateAndDept(HttpServletRequest request,String date) {
		RestServiceResult<Map> result = new RestServiceResult<Map>();
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DissentInfoController#getList", "未获取到当前用户信息！");
				return result;
			}

			result.setData(doublePublicityService.countSumByDept(date));
			return result;
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;

		}
	}

	@RequestMapping(value = "/queryallDataByDeptName.do")
	public RestServiceResult<Object> queryallDataByDeptName() {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			result.setData(doublePublicityService.queryallDataByDeptName());
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("数据异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("任务提醒定时任务DoublePublicityController#queryallDataByDeptName.do",
					"任务提醒定时任务异常！ERROR:" + e.getMessage(), e);

			return result;
		}

	}
	@IgnoreAuthority
	@RequestMapping(value = "/queryallHhDataByDeptName.do")
	public RestServiceResult<Object> queryallHhDataByDeptName() {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			result.setData(doublePublicityService.queryallHhDataByDeptName());
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("数据异常！ERROR:" + e.getMessage());
			ExceptionLogger.error("任务提醒定时任务DoublePublicityController#queryallHhDataByDeptName.do",
					"任务提醒定时任务异常！ERROR:" + e.getMessage(), e);

			return result;
		}

	}

	@IgnoreAuthority
	@RequestMapping(value = "/queryDoubleDataForPage.do")
	public RestServiceResult<Object> queryFrcfDataByA000001AndA000002ForPage(
			@RequestParam(value = "params1", defaultValue = "") String params1,
			@RequestParam(value = "params2", defaultValue = "") String params2,
			@RequestParam(value = "tableName", defaultValue = "法人行政处罚") String tableName, Page page) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			result = doublePublicityService.queryDoubleDataForPage(params1, params2, page, tableName);
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			ExceptionLogger.error("DoublePublicityController#queryDoubleDataForPage.do",
					"获取数据异常！ERROR：" + e.getMessage(), e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取数据异常！");
		}
		return result;
	}

	@RequestMapping(value = "/selectFrxkByColumnForPage.do")
	public RestServiceResult<Object> selectFrxkByColumnForPage(
			@RequestParam(value = "params1", defaultValue = "") String params1,
			@RequestParam(value = "params2", defaultValue = "") String params2,
			@RequestParam(value = "record_id", defaultValue = "") String record_id,
			@RequestParam(value = "tableName", defaultValue = "法人行政处罚") String tableName, Page page

	) {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			result = doublePublicityService.selectFrxkByColumnForPage(params1, params2, tableName, page,record_id);
			result.setCount(page.getTotalCount());
		} catch (Exception e) {
			ExceptionLogger.error("DoublePublicityController#selectFrxkByColumnForPage.do",
					"获取数据异常！ERROR：" + e.getMessage(), e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("获取数据异常！");
		}
		return result;
	}
}